package com.colin.library.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by Administrator on 2017/4/19.
 */

@SuppressLint({"ViewConstructor", "AppCompatCustomView"})
public class FloatView extends LinearLayout {
    private LinearLayout linear_float;
    private ImageView image_float;
    private long autoCloseTime = 4 * 1000;
    private boolean open = true;
    private boolean showAnimtioning = false;

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    public long getAutoCloseTime() {
        return autoCloseTime;
    }

    public void setAutoCloseTime(long autoCloseTime) {
        this.autoCloseTime = autoCloseTime;
    }


    public FloatView(Context context) {
        this(context, null, 0);
    }

    public FloatView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (null == linear_float || null == image_float) {
            initView();
        }
        this.postDelayed(new Runnable() {
            @Override
            public void run() {
                changeState();
            }
        }, autoCloseTime);

    }

    private void initView() {
        this.linear_float = (LinearLayout) this.findViewById(R.id.linear_float);
        this.image_float = (ImageView) this.findViewById(R.id.image_float);
    }

    @Override
    protected void onDetachedFromWindow() {
        open = true;
        showAnimtioning = false;
        super.onDetachedFromWindow();
    }

    /**
     * 动画效果实现隐藏一般
     */
    public void changeState() {
        open = !open;
        changView(open);
        if (open) {
            this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    changeState();
                }
            }, autoCloseTime);
        }
    }


    public void slideView(final float fromX, final float toX) {
        if (showAnimtioning) {
            return;
        }
        showAnimtioning = true;
        TranslateAnimation animation = new TranslateAnimation(fromX, toX, 0, 0);
        animation.setInterpolator(new AccelerateDecelerateInterpolator());
        animation.setDuration(300);
        animation.setFillBefore(false);
        animation.setFillEnabled(false);
        animation.setFillAfter(true);
        animation.setZAdjustment(Animation.ZORDER_TOP);
        animation.setStartOffset(0);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //清除动画 改变物理位置
                int distance = (int) (toX - fromX);
                FloatView.this.clearAnimation();
                FloatView.this.layout(FloatView.this.getLeft() + distance
                        , FloatView.this.getTop()
                        , FloatView.this.getRight() + distance
                        , FloatView.this.getBottom());
                showAnimtioning = false;
            }
        });
        FloatView.this.startAnimation(animation);
    }


    private void changView(final boolean open) {
        //View是否显示的标志
        //属性动画对象
        ValueAnimator valueAnimator;
        int width = this.getLayoutParams().width;
        final int newwidth = open ? width * 4 : width / 4;
        if (open) {
            //显示view，高度从0变到width值
            valueAnimator = ValueAnimator.ofInt(width, newwidth);
        } else {
            //隐藏view，高度从width变为0
            valueAnimator = ValueAnimator.ofInt(width, newwidth);
        }
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //获取当前的height值
                int w = (Integer) valueAnimator.getAnimatedValue();
                //动态更新view的高度
                FloatView.this.getLayoutParams().width = newwidth;
                linear_float.setSelected(!open);
                image_float.setSelected(!open);
                FloatView.this.requestLayout();
            }
        });
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        //开始动画
        valueAnimator.start();
    }
}
